import {Component, OnInit, ViewChild} from '@angular/core';
import {LocalStorage} from "../../app.local-storage";
import {UserModel} from "../../model/user.model";
import {CONSTANT} from "../../service/constant.service";
import {IMAGEAPI} from "../../service/Image.api.service";
import {UserInfo} from "../../service/userinfo.service";
import {USER} from "../../service/user.service";
import {ModifyTelphoneModalViewComponent} from "../modify-telphone-modal-view/modify-telphone-modal-view.component";
import {ModifyPasswordViewComponent} from "../modify-password-view/modify-password-view.component";
declare var $: any;

@Component({
  selector: 'app-my-profile-view',
  templateUrl: 'my-profile-view.component.html',
  styleUrls: ['my-profile-view.component.scss']
})
export class MyProfileViewComponent implements OnInit {
  @ViewChild(ModifyTelphoneModalViewComponent) modifyTelPhoneModal:any;
  @ViewChild(ModifyPasswordViewComponent) modifyPasswordModal:any;
  pensonalInfo: UserModel;
  constructor(private localStorage: LocalStorage,
              private constant: CONSTANT,
              private ImageApi: IMAGEAPI,
              private USER:USER,
              private userInfo:UserInfo) {
    this.pensonalInfo = this.USER.get();
  }


  // 读取本地照片
  handleFile() {
    let that: any = this;
    let file = $('#fileList')[0];
    let reader = new FileReader();
    let formData = new FormData(document.forms.namedItem("fileinfo"));
    reader.readAsDataURL(file.files[0]);
    reader.onload = function () {
      that.pensonalInfo.portrait= reader.result
      // $('#imgSrc').attr('src', reader.result);
      that.ImageApi.imageUpload(formData,(res:any)=>{
        console.log(res);
        that.pensonalInfo.portrait = res.saveUrl;
        let user = that.USER.get();
        user.portrait = res.saveUrl;
        console.log("update user");
        that.userInfo.updatePortrait({portrait:res.saveUrl},v=>that.USER.update(user))
      })
    }
  }

  ngOnInit() {

  }

  modifyTelPhone(){
    // TODO 修改手机号逻辑
    // this.modifyTelPhoneModal.showModal();
  }
  modifyPassword(){
    this.modifyPasswordModal.showModal();
  }
}

